<template>
    <admin-frame>
        <header class="page-header">
            <div class="flex-fill">
                <el-breadcrumb separator="/">
                    <el-breadcrumb-item>会务管理</el-breadcrumb-item>
                    <el-breadcrumb-item>体验活动</el-breadcrumb-item>
                    <el-breadcrumb-item>编辑活动</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div>
                <router-link to="/metting/activity/list">
                    <el-button type="primary" size="small">返回列表</el-button>
                </router-link>
            </div>
        </header>
        <div class="mainframe-content">
            <div class="content-block">
                <table class="dsxui-formtable">
                    <tbody>
                    <tr>
                        <td class="w60">活动名称</td>
                        <td>
                            <el-input size="medium" class="w300" v-model="activity.title"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>人数限制</td>
                        <td>
                            <el-input size="medium" class="w300" v-model="activity.num"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>截止日期</td>
                        <td>
                            <el-date-picker
                                    v-model="activity.closing_at"
                                    type="datetime"
                                    placeholder="选择日期时间"
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    format="yyyy-MM-dd HH:mm:ss"
                                    size="medium"
                                    class="w300"
                            >
                            </el-date-picker>
                        </td>
                    </tr>
                    <tr>
                        <td>活动海报</td>
                        <td>
                            <div class="w120" @click="showPicker=true">
                                <el-image :src="activity.image" fit="cover" class="img-110"
                                          v-if="activity.image"></el-image>
                                <div class="img-110 img-placeholder" v-else></div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td class="w60">活动介绍</td>
                        <td>
                            <kind-editor v-model="activity.content"></kind-editor>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="h40"></div>
        <div class="edit-bottom">
            <el-button class="w100" @click="$router.go(-1)">取消</el-button>
            <el-button class="w100" type="primary" @click="handleSubmit">保存</el-button>
        </div>
        <image-picker v-model="showPicker" @confirm="handlePickedImage"></image-picker>
    </admin-frame>
</template>

<script>
    import AdminFrame from "../common/AdminFrame";
    import KindEditor from "../../lib/KindEditor";

    export default {
        name: "ActivityEdit",
        components: {
            AdminFrame,
            KindEditor,
        },
        data() {
            return {
                id: 0,
                activity: {},
                showPicker: false
            }
        },
        mounted() {
            this.id = this.$route.query.id || 0;
            if (this.id) this.fetchData();
        },
        methods: {
            fetchData() {
                this.$get('/admin/meeting/activity/get?id=' + this.id).then(response => {
                    this.activity = response.data.activity;
                });
            },
            handlePickedImage(data) {
                this.activity.image = data.image;
            },
            handleSubmit() {
                const activity = this.activity;
                if (!activity.title) {
                    this.$showToast('请填写名称');
                    return false;
                }

                if (!activity.num) {
                    this.$showToast('请填写人数限制');
                    return false;
                }

                if (!activity.closing_at) {
                    this.$showToast('请选择截止时间');
                    return false;
                }

                if (!activity.image) {
                    this.$showToast('请上传海报');
                    return false;
                }

                const id = this.id;
                this.$post('/admin/meeting/activity/save', {id, activity}).then(response => {
                    this.$showToast('信息保存成功!', () => this.$router.go(0));
                });
            }
        }
    }
</script>

<style scoped>

</style>
